package cn.scau.server.controller;

import cn.scau.server.pojo.Dept;
import cn.scau.server.pojo.EmpPos;
import cn.scau.server.service.Component;
import cn.scau.server.service.DeptService;
import cn.scau.server.service.EmpService;
import cn.scau.server.vo.EmpPosDeptVO;
import cn.scau.server.vo.ResultVO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.scau.server.pojo.Emp;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 代码不跑我先跑
 * @since 2025-03-30
 */
@RestController
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private DeptService deptService;

    //获取部门树
    @GetMapping("/buildTree")
    public ResultVO<Component> getDeptTree() {
        Component tree = deptService.buildTree();
        if (tree != null) {
            return ResultVO.success("获取部门树成功", tree);
        } else {
            return ResultVO.fail("获取部门树失败");
        }
    }

    //添加部门
    @PostMapping("/addDept")
    public ResultVO addDept(String name, String parentName) {
        return deptService.addDept(name,parentName);
    }

    //根据部门名称删除部门
    @PostMapping("/deleteDeptByName")
    public ResultVO deleteDeptByName(String name) {
        return deptService.deleteDeptByName(name);
    }

    // 查找所有部门
    @GetMapping("/getAllDepts")
    public ResultVO<List<Dept>> getAllDepts() {
        List<Dept> depts = deptService.list();
        return ResultVO.success("查询成功",depts);
    }

    // 查找当前部门下的所有员工信息
    @GetMapping("/getEmployeesUnderDept")
    public ResultVO<List<EmpPosDeptVO>> getEmployeesUnderDept(String deptName) {
        return deptService.getEmpsUnderDept(deptName);
    }

    // 获取部门树层高
    @GetMapping("/getDeptTreeHeight")
    public ResultVO getDeptTreeHeight() {
        return ResultVO.success("查询成功",deptService.getDeptTreeHeight());
    }

    //根据当前员工ID获取所属部门及其所有子部门的信息
    @GetMapping("/getSubDeptIds")
    @ApiOperation(value = "获取所属部门及子部门信息", notes = "根据当前员工ID获取所属部门及其所有子部门的信息")
    public ResultVO<List<Dept>> getSubDeptIds() {
        return deptService.collectAllSubDeptsByEmpId();
    }

    //通过部门ID查询对应的部门名称
    @GetMapping("/getDeptNameById")
    @ApiOperation(value = "根据部门ID获取部门名称", notes = "通过部门ID查询对应的部门名称")
    @ApiImplicitParam(name = "deptId", value = "部门ID", required = true, dataType = "int")
    public ResultVO<String> getDeptNameById(Integer deptId) {
        return deptService.getDeptNameById(deptId);
    }
}
